// pages/zhuye/zhuye.js
import {
    request
} from "../../Tool/request/index";
import {
    format
} from "../../Tool/Time/index"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        canIUseGetUserProfile: false,
        canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
        santian_data: [0, 0, 0],
        city_info: {},
        tianqi_info: {},
        shenghuozhishu: {},
        richuriluo: {}
    },

    handleSearchVal(e) {
        const search_val = e.detail
        if (search_val == undefined) {
            return
        }
        request({
                url: "https://geoapi.qweather.com/v2/city/lookup?location=" + search_val
            })
            .then((res) => {
                this.setData({
                    city_info: res.data.location[0]
                })
                var longitude = this.data.city_info.lon
                var latitude = this.data.city_info.lat
                request({
                        url: "https://geoapi.qweather.com/v2/city/lookup?location=" + longitude + "," + latitude
                    })
                    .then((res) => {
                        this.setData({
                            city_info: res.data.location[0]
                        })
                    })
                request({
                        url: "https://devapi.qweather.com/v7/weather/now?location=" + longitude + "," + latitude
                    })
                    .then((res) => {
                        var date = new Date(res.data.now.obsTime)
                        res.data.now.obsTime = format(date, "MM-dd hh:mm")
                        // console.log(res.data.now)
                        this.setData({
                            tianqi_info: res.data.now
                        })
                    })
                var date_time = format(new Date(), "yyyyMMdd")
                request({
                        url: "https://devapi.qweather.com/v7/astronomy/sun?location=" + longitude + "," + latitude + "&date=" + date_time
                    })
                    .then((res) => {
                        // console.log(res.data)
                        res.data.sunrise = format(new Date(res.data.sunrise), "MM-dd hh:mm")
                        res.data.sunset = format(new Date(res.data.sunset), "MM-dd hh:mm")
                        // console.log(res.data)
                        this.setData({
                            richuriluo: res.data
                        })
                    })
                request({
                        url: "https://devapi.qweather.com/v7/weather/3d?location=" + longitude + "," + latitude
                    })
                    .then((res) => {
                        // console.log(res.data.daily)
                        this.setData({
                            santian_data: res.data.daily
                        })
                    })
                request({
                        url: "https://devapi.qweather.com/v7/indices/1d?location=" + longitude + "," + latitude + "&type=0"
                    })
                    .then((res) => {
                        console.log(res.data.daily)
                        this.setData({
                            shenghuozhishu: res.data.daily
                        })
                    })
            })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                        success: res => {
                            console.log(res)
                        }
                    })
                } else {
                    this.setData({
                        ismask: 'block'
                    });
                }
            }
        });

        wx.setNavigationBarTitle({
            title: '天气'
        })
        wx.setNavigationBarColor({
                frontColor: '#ffffff', // 必写项
                backgroundColor: '#1296db', // 传递的颜色值
                animation: { // 可选项，加上这项会有个显示的动画效果
                    duration: 1000,
                    timingFunc: 'easeIn'
                }
            }),
            wx.getLocation({
                type: 'wgs84',
                success: (res) => {
                    var latitude = res.latitude // 纬度
                    var longitude = res.longitude // 经度
                    request({
                            url: "https://geoapi.qweather.com/v2/city/lookup?location=" + longitude + "," + latitude
                        })
                        .then((res) => {
                            this.setData({
                                city_info: res.data.location[0]
                            })
                        })
                    request({
                            url: "https://devapi.qweather.com/v7/weather/now?location=" + longitude + "," + latitude
                        })
                        .then((res) => {
                            var date = new Date(res.data.now.obsTime)
                            res.data.now.obsTime = format(date, "MM-dd hh:mm")
                            console.log(res.data.now)
                            this.setData({
                                tianqi_info: res.data.now
                            })
                        })
                    var date_time = format(new Date(), "yyyyMMdd")
                    request({
                            url: "https://devapi.qweather.com/v7/astronomy/sun?location=" + longitude + "," + latitude + "&date=" + date_time
                        })
                        .then((res) => {
                            // console.log(res.data)
                            res.data.sunrise = format(new Date(res.data.sunrise), "MM-dd hh:mm")
                            res.data.sunset = format(new Date(res.data.sunset), "MM-dd hh:mm")
                            // console.log(res.data)
                            this.setData({
                                richuriluo: res.data
                            })
                        })
                    request({
                            url: "https://devapi.qweather.com/v7/weather/3d?location=" + longitude + "," + latitude
                        })
                        .then((res) => {
                            // console.log(res.data.daily)
                            this.setData({
                                santian_data: res.data.daily
                            })
                        })
                    request({
                            url: "https://devapi.qweather.com/v7/indices/1d?location=" + longitude + "," + latitude + "&type=0"
                        })
                        .then((res) => {
                            // console.log(res.data.daily)
                            this.setData({
                                shenghuozhishu: res.data.daily
                            })
                        })
                }
            })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})